home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ASTRONOM / H139.ZIP / UI101.ZIP / IO / UI / UI_RBAND.C < prev    next >
C/C++ Source or Header  |  1991-11-04  |  11KB  |  410 lines

  1.  /***************************************************************
  2.  
  3.     ui_rband.c      rubber-band routine for ui
  4.  
  5.             Copyright (c) 1991, Ted A. Campbell
  6.  
  7.             Bywater Software
  8.             P. O. Box 4023 
  9.             Duke Station 
  10.             Durham, NC  27706
  11.  
  12.             email: tcamp@hercules.acpub.duke.edu
  13.  
  14.     Copyright and Permissions Information:
  15.  
  16.     All U.S. and international copyrights are claimed by the
  17.     author. The author grants permission to use this code
  18.     and software based on it under the following conditions:
  19.     (a) in general, the code and software based upon it may be 
  20.     used by individuals and by non-profit organizations; (b) it
  21.     may also be utilized by governmental agencies in any country,
  22.     with the exception of military agencies; (c) the code and/or
  23.     software based upon it may not be sold for a profit without
  24.     an explicit and specific permission from the author, except
  25.     that a minimal fee may be charged for media on which it is
  26.     copied, and for copying and handling; (d) the code must be 
  27.     distributed in the form in which it has been released by the
  28.     author; and (e) the code and software based upon it may not 
  29.     be used for illegal activities. 
  30.  
  31. ***************************************************************/
  32.  
  33. #include "stdio.h"
  34. #include "ctype.h"
  35.  
  36. #include "bw.h"
  37. #include "kb.h"
  38. #include "gr.h"
  39. #include "ui.h"
  40.  
  41. #ifdef __STDC__
  42. #include "malloc.h"
  43. #else
  44. extern char * malloc();
  45. #endif
  46.  
  47. #ifndef   __STDC__
  48. #define   time_t    long
  49. #define   size_t   int
  50. #endif
  51.  
  52. #define ERR_RBAND       "Memory error in ui_rband() routine"
  53.  
  54. ui_rband( mode, x1, y1, x2, y2 )
  55.    int mode;
  56.    int *x1, *y1, *x2, *y2;
  57.    {
  58.    static int left, right, top, bottom;
  59.    static int b, k;
  60.    static int y, x;             /* current position */
  61.    static int yy, xx;           /* dummy arguments */
  62.    int start_x, start_y;        /* start x, y, position */
  63.    int prev_x, prev_y;          /* previous x, y position */
  64.    int rxstat;                  /* receive status (keyboard or mouse) */
  65.  
  66.    /* Check mode */
  67.  
  68.    if ( mode != TRUE )
  69.       {
  70.       goto movebox;
  71.       }
  72.  
  73.    /*** RBAND FUNCTION TRUE: RUBBERBAND FOR VARIABLE_SIZED BOX ****/
  74.  
  75.    /* save initial screen areas */
  76.  
  77.    x = *x2;
  78.    y = *y1;
  79.    gr_imsave( GR_PRIMARY, TRUE, *x1, *y1, *x1, *y2, &left );
  80.    gr_imsave( GR_PRIMARY, TRUE, *x2, *y1, *x2, *y2, &right );
  81.    gr_imsave( GR_PRIMARY, TRUE, *x1, *y2, *x2, *y2, &top );
  82.    gr_imsave( GR_PRIMARY, TRUE, *x1, *y1, *x2, *y1, &bottom );
  83.  
  84.    /* reposition mouse to begin sizing */
  85.  
  86.    if ( gr_ismouse == TRUE )
  87.       {
  88.       gr_mouse( POSITION, &x, &y, &b );
  89.       }
  90.  
  91.    /* draw the initial box */
  92.  
  93.    gr_line( GR_PRIMARY, *x1, *y1, *x1, *y2, BLACK, SOLID );               /* left */
  94.    gr_line( GR_PRIMARY, *x2, *y1, *x2, *y2, BLACK, SOLID );               /* right */
  95.    gr_line( GR_PRIMARY, *x1, *y2, *x2, *y2, BLACK, SOLID );               /* top */
  96.    gr_line( GR_PRIMARY, *x1, *y1, *x2, *y1, BLACK, SOLID );               /* bottom */
  97.  
  98. #ifdef  OLD_DEBUG
  99.    sprintf( bw_ebuf, "gr_mouse( SAMPLE ) == %d     ",
  100.       gr_mouse( SAMPLE, x2, y1, &b ) );
  101.    bw_message( bw_ebuf );
  102. #endif
  103.  
  104.    /* main rband loop: wait until first mouse click or RETURN */
  105.  
  106.    if ( gr_ismouse == TRUE )
  107.       {
  108.       bw_message( " Use Mouse to Size Window, Click to Fix Size " );
  109.       }
  110.    else
  111.       {
  112.       bw_message( " Use Arrow Keys to Size Window, RETURN to Fix Size " );
  113.       }
  114.  
  115.    rxstat = FALSE;
  116.    while ( rxstat != TRUE )
  117.       {
  118.  
  119.       if (( x != *x2 ) || ( y != *y1 ))
  120.      {
  121.  
  122.      /* restore old screen areas */
  123.  
  124.      gr_imsave( GR_PRIMARY, FALSE, *x1, y, *x1, *y2, &left );
  125.      gr_imsave( GR_PRIMARY, FALSE, x, y, x, *y2, &right );
  126.      gr_imsave( GR_PRIMARY, FALSE, *x1, *y2, x, *y2, &top );
  127.      gr_imsave( GR_PRIMARY, FALSE, *x1, y, x, y, &bottom );
  128.  
  129.      /* release memory */
  130.  
  131.      gr_imfree( top );
  132.      gr_imfree( left );
  133.      gr_imfree( bottom );
  134.      gr_imfree( right );
  135.  
  136.      /* check for position above or left of point */
  137.  
  138.      if ( *x2 < *x1 )
  139.         {
  140.         *x2 = *x1;
  141.         }
  142.  
  143.      if ( *y1 > *y2 )
  144.         {
  145.         *y1 = *y2;
  146.         }
  147.  
  148.      /* save new screen areas */
  149.  
  150.      gr_imsave( GR_PRIMARY, TRUE, *x1, *y1, *x1, *y2, &left );
  151.      gr_imsave( GR_PRIMARY, TRUE, *x2, *y1, *x2, *y2, &right );
  152.      gr_imsave( GR_PRIMARY, TRUE, *x1, *y2, *x2, *y2, &top );
  153.      gr_imsave( GR_PRIMARY, TRUE, *x1, *y1, *x2, *y1, &bottom );
  154.  
  155.      /* draw lines */
  156.  
  157. #ifdef OLD_DEBUG
  158.          bw_message( "Drawing...      " );
  159. #endif
  160.  
  161.      gr_line( GR_PRIMARY, *x1, *y1, *x1, *y2, BLACK, SOLID );               /* left */
  162.      gr_line( GR_PRIMARY, *x2, *y1, *x2, *y2, BLACK, SOLID );               /* right */
  163.      gr_line( GR_PRIMARY, *x1, *y2, *x2, *y2, BLACK, SOLID );               /* top */
  164.      gr_line( GR_PRIMARY, *x1, *y1, *x2, *y1, BLACK, SOLID );               /* bottom */
  165.  
  166.      x = *x2;
  167.      y = *y1;
  168.      }
  169. #ifdef OLD_DEBUG
  170.       else
  171.          {
  172.          bw_message( "No change...      " );
  173.          }
  174. #endif
  175.  
  176.       /* check keyboard or mouse status */
  177.  
  178.       if ( gr_ismouse == TRUE )
  179.      {
  180.      rxstat = gr_mouse( SAMPLE, x2, y1, &b );
  181.      }
  182.       else
  183.      {
  184.      if ( kb_rxstat() == TRUE )
  185.         {
  186.         k = kb_rx();
  187.         switch (k)
  188.            {
  189.            case '\n':
  190.            case '\r':
  191.           rxstat = TRUE;
  192.           break;
  193.            case KB_UP:
  194.           ++(*y1);
  195.           break;
  196.            case KB_DOWN:
  197.           --(*y1);
  198.           break;
  199.            case KB_LEFT:
  200.           --(*x2);
  201.           break;
  202.            case KB_RIGHT:
  203.           ++(*x2);
  204.           break;
  205.            }
  206.         }
  207.      }
  208.  
  209.       }                         /* end main rband loop */
  210.  
  211.    /* wait until the mouse is released */
  212.  
  213.    if ( gr_ismouse == TRUE )
  214.       {
  215.       gr_mouse( WAIT, &xx, &yy, &b );                /* down */
  216.       gr_mouse( WAIT, &xx, &yy, &b );                /* up */
  217.       }
  218.  
  219. #ifdef  OLD_DEBUG
  220.    bw_message( "Loop Finished     " );
  221. #endif
  222.  
  223.    /* restore old screen areas */
  224.  
  225.    gr_imsave( GR_PRIMARY, FALSE, *x1, y, *x1, *y2, &left );
  226.    gr_imsave( GR_PRIMARY, FALSE, x, y, x, *y2, &right );
  227.    gr_imsave( GR_PRIMARY, FALSE, *x1, *y2, x, *y2, &top );
  228.    gr_imsave( GR_PRIMARY, FALSE, *x1, y, x, y, &bottom );
  229.  
  230.    /* release memory */
  231.  
  232.    gr_imfree( top );
  233.    gr_imfree( left );
  234.    gr_imfree( bottom );
  235.    gr_imfree( right );
  236.  
  237.    goto end;
  238.  
  239.    /*** RBAND FUNCTION NOT TRUE: MOVE FIXED_SIZE BOX ***/
  240.  
  241.    movebox:
  242.  
  243.    /* reposition the mouse */
  244.  
  245.    start_x = x = *x2;
  246.    start_y = y = *y1;
  247.  
  248.    if ( gr_ismouse == TRUE )
  249.       {
  250.       gr_mouse( POSITION, &x, &y, &b );
  251.       }
  252.  
  253.    /* save the initial area */
  254.  
  255.    gr_imsave( GR_PRIMARY, TRUE, *x1, *y1, *x1, *y2, &left );
  256.    gr_imsave( GR_PRIMARY, TRUE, *x2, *y1, *x2, *y2, &right );
  257.    gr_imsave( GR_PRIMARY, TRUE, *x1, *y2, *x2, *y2, &top );
  258.    gr_imsave( GR_PRIMARY, TRUE, *x1, *y1, *x2, *y1, &bottom );
  259.  
  260.    /* show the initial rectangle */
  261.  
  262.    gr_line( GR_PRIMARY, *x1, *y1, *x1, *y2, BLACK, SOLID );
  263.    gr_line( GR_PRIMARY, *x2, *y1, *x2, *y2, BLACK, SOLID );
  264.    gr_line( GR_PRIMARY, *x1, *y2, *x2, *y2, BLACK, SOLID );
  265.    gr_line( GR_PRIMARY, *x1, *y1, *x2, *y1, BLACK, SOLID );
  266.  
  267.    /* main rband loop */
  268.  
  269.    prev_x = x = start_x;
  270.    prev_y = y = start_y;
  271.  
  272.    if ( gr_ismouse == TRUE )
  273.       {
  274.       bw_message( " Use Mouse to Move Window, Click to Fix Position " );
  275.       }
  276.    else
  277.       {
  278.       bw_message( " Use Arrow Keys to Move Window, RETURN to Fix Position " );
  279.       }
  280.  
  281.    rxstat = FALSE;
  282.    while( rxstat != TRUE )
  283.       {
  284.  
  285.       if ( ( x != prev_x ) || ( y != prev_y ))
  286.      {
  287.  
  288.      /* restore the old area */
  289.  
  290.      gr_imsave( GR_PRIMARY, FALSE, *x1 + ( prev_x - start_x ), *y1 + ( prev_y - start_y ), *x1 + ( prev_x - start_x ), *y2 + ( prev_y - start_y ), &left );
  291.      gr_imsave( GR_PRIMARY, FALSE, *x2 + ( prev_x - start_x ), *y1 + ( prev_y - start_y ), *x2 + ( prev_x - start_x ), *y2 + ( prev_y - start_y ), &right );
  292.      gr_imsave( GR_PRIMARY, FALSE, *x1 + ( prev_x - start_x ), *y2 + ( prev_y - start_y ), *x2 + ( prev_x - start_x ), *y2 + ( prev_y - start_y ), &top );
  293.      gr_imsave( GR_PRIMARY, FALSE, *x1 + ( prev_x - start_x ), *y1 + ( prev_y - start_y ), *x2 + ( prev_x - start_x ), *y1 + ( prev_y - start_y ), &bottom );
  294.  
  295.      /* release memory */
  296.  
  297.      gr_imfree( top );
  298.      gr_imfree( left );
  299.      gr_imfree( bottom );
  300.      gr_imfree( right );
  301.  
  302.      /* check for position out of bounds */
  303.  
  304.      if ( ( *x1 + ( x - start_x )) < 0 )
  305.          {
  306.          x = prev_x;
  307.          }
  308.      else if ( ( *x2 + ( x - start_x )) > ui_grwind->xmax )
  309.          {
  310.          x = prev_x;
  311.          }
  312.  
  313.      if ( ( *y1 + ( y - start_y )) < 0 )
  314.          {
  315.          y = prev_y;
  316.          }
  317.      else if ( ( *y2 + ( y - start_y )) > ui_grwind->ymax )
  318.          {
  319.          y = prev_y;
  320.          }
  321.  
  322.      /* save the new area */
  323.  
  324.      gr_imsave( GR_PRIMARY, TRUE, *x1 + ( x - start_x ), *y1 + ( y - start_y ), *x1 + ( x - start_x ), *y2 + ( y - start_y ), &left );
  325.      gr_imsave( GR_PRIMARY, TRUE, *x2 + ( x - start_x ), *y1 + ( y - start_y ), *x2 + ( x - start_x ), *y2 + ( y - start_y ), &right );
  326.      gr_imsave( GR_PRIMARY, TRUE, *x1 + ( x - start_x ), *y2 + ( y - start_y ), *x2 + ( x - start_x ), *y2 + ( y - start_y ), &top );
  327.      gr_imsave( GR_PRIMARY, TRUE, *x1 + ( x - start_x ), *y1 + ( y - start_y ), *x2 + ( x - start_x ), *y1 + ( y - start_y ), &bottom );
  328.  
  329.      /* draw the new area */
  330.  
  331.      gr_line( GR_PRIMARY, *x1 + ( x - start_x ), *y1 + ( y - start_y ), *x1 + ( x - start_x ), *y2 + ( y - start_y ), BLACK, SOLID );
  332.      gr_line( GR_PRIMARY, *x2 + ( x - start_x ), *y1 + ( y - start_y ), *x2 + ( x - start_x ), *y2 + ( y - start_y ), BLACK, SOLID );
  333.      gr_line( GR_PRIMARY, *x1 + ( x - start_x ), *y2 + ( y - start_y ), *x2 + ( x - start_x ), *y2 + ( y - start_y ), BLACK, SOLID );
  334.      gr_line( GR_PRIMARY, *x1 + ( x - start_x ), *y1 + ( y - start_y ), *x2 + ( x - start_x ), *y1 + ( y - start_y ), BLACK, SOLID );
  335.  
  336.      prev_x = x;
  337.      prev_y = y;
  338.      }
  339.  
  340.       /* check keyboard or mouse status */
  341.  
  342.       if ( gr_ismouse == TRUE )
  343.      {
  344.      rxstat = gr_mouse( SAMPLE, &x, &y, &b );
  345.      }
  346.       else
  347.      {
  348.      if ( kb_rxstat() == TRUE )
  349.         {
  350.         k = kb_rx();
  351.         switch (k)
  352.            {
  353.            case '\n':
  354.            case '\r':
  355.           rxstat = TRUE;
  356.           break;
  357.            case KB_UP:
  358.           ++y;
  359.           break;
  360.            case KB_DOWN:
  361.           --y;
  362.           break;
  363.            case KB_LEFT:
  364.           --x;
  365.           break;
  366.            case KB_RIGHT:
  367.           ++x;
  368.           break;
  369.            }
  370.         }
  371.      }
  372.  
  373.       }                                 /* end of main rband loop */
  374.  
  375.    /* restore the old area */
  376.  
  377.    gr_imsave( GR_PRIMARY, FALSE, *x1 + ( prev_x - start_x ), *y1 + ( prev_y - start_y ), *x1 + ( prev_x - start_x ), *y2 + ( prev_y - start_y ), &left );
  378.    gr_imsave( GR_PRIMARY, FALSE, *x2 + ( prev_x - start_x ), *y1 + ( prev_y - start_y ), *x2 + ( prev_x - start_x ), *y2 + ( prev_y - start_y ), &right );
  379.    gr_imsave( GR_PRIMARY, FALSE, *x1 + ( prev_x - start_x ), *y2 + ( prev_y - start_y ), *x2 + ( prev_x - start_x ), *y2 + ( prev_y - start_y ), &top );
  380.    gr_imsave( GR_PRIMARY, FALSE, *x1 + ( prev_x - start_x ), *y1 + ( prev_y - start_y ), *x2 + ( prev_x - start_x ), *y1 + ( prev_y - start_y ), &bottom );
  381.  
  382.    /* release memory */
  383.  
  384.    gr_imfree( top );
  385.    gr_imfree( left );
  386.    gr_imfree( bottom );
  387.    gr_imfree( right );
  388.  
  389.    /* get final mouse position */
  390.  
  391.    if ( gr_ismouse == TRUE )
  392.       {
  393.       gr_mouse( WAIT, &x, &y, &b );        /* click down */
  394.       gr_mouse( WAIT, &xx, &yy, &b );        /* click up */
  395.       }
  396.  
  397.    *x1 = *x1 + ( x - start_x );
  398.    *x2 = *x2 + ( x - start_x );
  399.    *y1 = *y1 + ( y - start_y );
  400.    *y2 = *y2 + ( y - start_y );
  401.  
  402.    end:
  403.  
  404.    bw_message( " " );
  405.  
  406.    return TRUE;
  407.  
  408.    }
  409.  
  410.